const is__d_call = require('./is__d_call');
const getPropValue = require('../common/utility.js').getPropValue;
const getObjectPropertyByKeyInAst = require('./getObjectPropertyInAst.js');
const get_dependencies_list_in___d_call = require('./get_dependencies_list_in___d_call');

const file = require('../common/file');

// 匹配项目中的pages.js文件
function parse_pages_from_pages_file(jsbundle_code,pages_code , ast ){
	//var error = false;
	var pages_in_first_file = [];
	var pages_in_second_file = [];

	if((typeof pages_code!=='string') || (!is__d_call(ast))){
		return;
	}

	var the_requires_array = get_dependencies_list_in___d_call(ast);// 返回值是Int数组
	var the_pages_list_ast = get_the_page_list_ast(ast);// the_pages_list_ast的内容参见"the_pages_list_ast.json"注释文件

	if((!the_pages_list_ast)||(!the_pages_list_ast.push)||(!the_requires_array)||(!the_requires_array.push)){
		return;
	}

	var params_list = getPropValue(ast,['expression','arguments',0,'params']);
	if((!params_list) || (!params_list.length))return;

	var params = [];
	for(var i=0; i<params_list.length; i++){
		var name = (params_list[i]||{}).name;
		if(!name)return;
		params.push(name);
	}

	var name_of_require_function = params[1];
	var name_of_list = params[4];
	if((!name_of_require_function)||(!name_of_list)){
		return;
	}

	//file.writeFileSync('./example_code/the_pages_object.json',JSON.stringify(the_pages_list_ast,null,4));

	var first_file_pages = [];
	var other_file_pages = [];
	var errors = [];
	for(var i=0; i<the_pages_list_ast.length; i++){
		var page_ast = the_pages_list_ast[i];
		var infos = parse_the_page_info(jsbundle_code, page_ast,name_of_require_function,name_of_list,the_requires_array);
		if((!infos)|| (typeof infos==='string')){

			var error = infos;
			console.log(error);
			errors.push(error||'unknow');
			break;
		} else {
			if(infos.is_in_first_file){
				first_file_pages = first_file_pages.concat(infos.all_requires);
			} else {
				other_file_pages = other_file_pages.concat(infos.all_requires);
			}
		}
	}

	// Note ： first_file_pages和other_file_pages必定有重叠，
	//         重叠部分应该仅保留在first_file_pages中，而other_file_pages中的应该删除

	// console.log(JSON.stringify({
	// 	first_file_pages:first_file_pages,
	// 	other_file_pages:other_file_pages
	// },null,4));

	var r = {
		first_file_pages:first_file_pages,
		other_file_pages:other_file_pages
	};
	return r;

	//file.writeFileSync('./example_code/split_requires.json',JSON.stringify(r,null,4));
}



// 解析单个的页面信息
// page_ast参数是类似下面代码的ast .
// 注意代码中的 t(u[21]) :
//  参数name_of_require_function表示t ，
//  参数name_of_list表示u
/*
*
  AboutUs: {
	 Page: function () {
	 	return t(u[21]).default
	 },
	 defaultRouteInfo: {
	 	title: '\u5173\u4e8e\u6211\u4eec'
	 }
  }
* */

function parse_the_page_info(jsbundle_code,page_ast,name_of_require_function,name_of_list,the_requires_array) {
	var check = check_page_define(page_ast);
	if(check!==true){
		return check;
	}

	var page_name = getPropValue(page_ast,['key','name']);
	if((typeof page_name!=='string') || (!page_name)){
		return '没有找到key';
	}

	var all_requires = [];
	var is_in_first_file = false;

	var properties = getPropValue(page_ast,['value','properties']);
	var Page_value_ast = getObjectPropertyByKeyInAst(properties,'Page');
	if(!Page_value_ast){
		return '页面'+page_name+'没有Page属性';
	}

	var start = Page_value_ast.start;
	var end = Page_value_ast.end;
	if(typeof start!=='number' || typeof end!=='number'){
		return '没有找到Page定义函数';
	}
	var code = jsbundle_code.substring(start,end);
	var str = "\\b"+name_of_require_function+"\\(" + name_of_list+"\\[\\d+\\]\\)";
	var reg = new RegExp(str,"g");
	var m = code.match(reg);

	if(m&&m.length&&m.push){
		for(var j=0; j<m.length; j++){
			var m_item = m[j];
			var m_number = m_item.match(/(\d+)/);
			if(m_number&&m_number[1]){
				all_requires.push(the_requires_array[parseInt(m_number[1],10)]);
			}
		}
	}

	var first_file_in_jsbundles_value_ast = getObjectPropertyByKeyInAst(properties,'first_file_in_jsbundles');
	// 布尔值true会被转为'!0'，所以这里不能直接判断===true
	// 所以，路由中的配置就是：只要存在first_file_in_jsbundles属性，无论值是什么，都会被解析为入口页
	if(first_file_in_jsbundles_value_ast/* && first_file_in_jsbundles_value_ast.value===true*/){
		is_in_first_file = true;
	}
	return {
		all_requires:all_requires,
		is_in_first_file:is_in_first_file

	};
}

// 判断是否是页面定义
// 要么返回true，要么返回一个string，描述错误内容
function check_page_define(page_ast) {
	/*
	if(!page_ast)return false;
	var type = page_ast.type;
	if(type!=='Property'){
		return false;
	}

	var value_type = getPropValue(page_ast,['value','type']);
	if((value_type!=='ObjectExpression')){
		return false;
	}
	*/
	var key_name = getPropValue(page_ast,['key','name']);
	if((typeof key_name!=='string') || (!key_name)){
		return '没有找到key';
	}

	var properties = getPropValue(page_ast,['value','properties']);
	// properties中要求必须有一个是'Page'属性
	if((!properties)||(!properties.length)){
		return '页面'+key_name+'没有Page属性';
	}
	var is_valid = false;
	for(var i=0; i<properties.length; i++){
		var property = properties[i];
		var key_name = getPropValue(property,['key','name']);
		if(key_name==='Page'){
			is_valid = true;
		}
	}
	if(is_valid){
		return true;
	} else {
		return '页面'+key_name+'没有Page属性';
	}
}

// 获取形如注释文件夹中the_pages_list_ast.json这样的结果
function get_the_page_list_ast(ast) {
	var the_pages_list_ast;

	var body = getPropValue(ast,['expression','arguments',0,'body','body']);
	if((!body)||(!body.length)){
		return 'pages列表ast获取失败';
	}
	for(var i=0; i<body.length; i++){
		var itm_ast = body[i];
		var type = getPropValue(itm_ast,['expression','type']);
		var property_name = getPropValue(itm_ast,['expression','left','property','name']);
		var right_value_type = getPropValue(itm_ast,['expression','right','type']);
		var properties = getPropValue(itm_ast,['expression','right','properties']);// 所有页面
		if(type==='AssignmentExpression' && property_name==='pages' && right_value_type==='ObjectExpression' && properties && properties.push){
			the_pages_list_ast = properties;
			break;
		}
	}
	return the_pages_list_ast;
}

module.exports = parse_pages_from_pages_file;

